//
//  RxLearningDVC.swift
//  CooperSwift_Example
//
//  Created by 计海峰 on 2021/5/20.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import UIKit
import RxSwift
import Then
import CooperSwift

class RxLearningDVC: UIViewController {
    
    let disposeBag = DisposeBag()

    let inputTF = UITextField.init().then {
        $0.font = .systemFont(ofSize: 15)
        $0.backgroundColor = .random()
    }
    
    let countLB = UILabel.init().then{
        $0.font = .systemFont(ofSize: 15)
        $0.textAlignment = .center
        $0.backgroundColor = .random()
    }
    
    let testBT = UIButton.init().then{
        $0.titleLabel?.font = .systemFont(ofSize: 15)
        $0.backgroundColor = .random()
        $0.setTitle("测试", for: .normal)
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        bgColor(.white)
       
        view.addSubviews([inputTF, countLB, testBT])
        
        inputTF.snp.makeConstraints {
            $0.top.equalTo(view).offset(Screen.statusBarHeight + Screen.navBarHeight + 40)
            $0.left.equalTo(view).offset(20)
            $0.right.equalTo(view).offset(-20)
            $0.height.equalTo(44)
        }
        
        countLB.snp.makeConstraints {
            $0.top.equalTo(inputTF.snp_bottomMargin).offset(20)
            $0.left.equalTo(view).offset(20)
            $0.right.equalTo(view).offset(-20)
            $0.height.equalTo(44)
        }
        
        testBT.snp.makeConstraints {
            $0.top.equalTo(countLB.snp_bottomMargin).offset(20)
            $0.left.equalTo(view).offset(20)
            $0.right.equalTo(view).offset(-20)
            $0.height.equalTo(44)
        }
        
        let state = inputTF.rx.text.asDriver()
        let observer = countLB.rx.text
        state.map{ $0!.count > 0 ? "\(String(describing: $0!)) -- \(String($0!.count))" : "" }.drive(observer).disposed(by: disposeBag)
        
        testBT.rx.tap.subscribe { [weak self] in
            self?.showAlert(title: "xxx", message: "123")
        } onError: { error in
            print("testBT tap onError \(error)")
        } onCompleted: {
            print("testBT tap onCompleted")
        } onDisposed: {
            print("testBT tap onDisposed")
        }.disposed(by: disposeBag)

        // Do any additional setup after loading the view.
        
        sessionObserverTest()
        
    }
    
    // AnyObserver
    func sessionObserverTest() {
        URLSession.shared.rx.data(request: URLRequest.init(url: URL.init(string: "https://wwww.baidu.com")!)).subscribe { data in
            print("session data \(data)")
        } onError: { error in
            print("session Observer \(error)")
        } onCompleted: {
            print("session Observer onCompleted")
        } onDisposed: {
            print("session Observer onDisposed")
        }.disposed(by: disposeBag)

    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
